import {
	WebSocketClient
} from "@/util/WebSocketClient";
import http from "@/util/http";
let ws;
let timer = null
export default {
	//防抖
	debounce(fn, delay) {
		return function() {
			if (timer) {
				clearTimeout(timer)
			}
			timer = setTimeout(fn, delay)
		}
	},
	//节流
	throttle(fn, delay) {
		let valid = true;
		return function() {
			if (!valid) {
				return false
			}
			valid = false
			setTimeout(() => {
				fn()
				valid = true;
			}, delay)
		}
	},
	//时间戳转时间，不传时间则获取当前时间
	getCurrentTime(time) {
		let d = '';
		if (time) {
			d = new Date(time);
		} else {
			d = new Date();
		}
		let obj = {
			Y: d.getFullYear(),
			M: d.getMonth() + 1,
			D: d.getDate(),
			H: d.getHours(),
			Mi: d.getMinutes(),
			S: d.getSeconds()
		}
		return obj.Y + "-" + this.handleNum(obj.M) + "-" + this.handleNum(obj.D);
		// +" "+this.handleNum(obj.H)+":"+this.handleNum(obj.Mi)+":"+this.handleNum(obj.S)
	},
	//时间戳转时间，不传时间则获取当前时间
	getCurrentTimeNew(day, time) {
		let d = '';
		if (time) {
			d = new Date(time);
		} else {
			d = new Date();
		}
		let obj = {
			Y: d.getFullYear(),
			M: d.getMonth() + 1,
			D: d.getDate() + day,
			H: d.getHours(),
			Mi: d.getMinutes(),
			S: d.getSeconds()
		}
		return obj.Y + "/" + this.handleNum(obj.M) + "/" + this.handleNum(obj.D) + " " + this.handleNum(obj.H) + ":" + this
			.handleNum(obj.Mi) + ":" + this.handleNum(obj.S);
	},
	weekDay(date) {
		let nowD = this.getCurrentTime().substr(0, 10);
		if (nowD == date) {
			return '今天'
		}
		var dt = new Date(date.split("/")[0], date.split("/")[1] - 1, date = date.split("/")[2]);
		var weekDay = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
		return weekDay[dt.getDay()];
	},
	getDaysBetween(date1, date2) {
		var startDate = Date.parse(date1);
		var endDate = Date.parse(date2);
		if (startDate > endDate) {
			return 0;
		}
		if (startDate == endDate) {
			return 1;
		}
		var days = Math.round((endDate - startDate) / (1 * 24 * 60 * 60 * 1000));
		return days;
	},
	//时间补零操作
	handleNum(num) {
		if (num < 10) {
			num = "0" + num
		}
		return num
	},
	// 中文
	chinese(value) {
		let reg = /^[\u4e00-\u9fa5]+$/gi;
		return reg.test(value);
	},
	//生成随机数
	random(min, max) {
		if (min >= 0 && max > 0 && max >= min) {
			let gab = max - min + 1;
			return Math.floor(Math.random() * gab + min);
		} else {
			return 0;
		}
	},
	handleTime(dateTime) {
		if (!dateTime) dateTime = Number(new Date());
		// 如果dateTime长度为10或者13，则为秒和毫秒的时间戳，如果超过13位，则为其他的时间格式
		if (dateTime.toString().length == 10) dateTime *= 1000;
		let timestamp = +new Date(Number(dateTime));

		let timer = (Number(new Date()) - timestamp) / 1000;
		// 如果小于5分钟,则返回"刚刚",其他以此类推
		let tips = '';
		switch (true) {
			case timer < 300:
				tips = '刚刚';
				break;
			case timer >= 300 && timer < 3600:
				tips = parseInt(timer / 60) + '分钟前';
				break;
			case timer >= 3600 && timer < 86400:
				tips = parseInt(timer / 3600) + '小时前';
				break;
			case timer >= 86400 && timer < 2592000:
				tips = parseInt(timer / 86400) + '天前';
				break;
			default:
				if (timer >= 2592000 && timer < 365 * 86400) {
					tips = parseInt(timer / (86400 * 30)) + '个月前';
				} else {
					tips = parseInt(timer / (86400 * 365)) + '年前';
				}

		}
		return tips;
	},
	getRect(selector) {
		return new Promise((resolve) => {
			let view = uni.createSelectorQuery().select(selector);
			view.fields({
				size: true,
				rect: true,
				scrollOffset: true
			}, (res) => {
				resolve(res);
			}).exec();
		})
	},
	handleDay(date) {
		let d = new Date(date);
		let obj = {
			Y: d.getFullYear(),
			M: d.getMonth() + 1,
			D: d.getDate(),
			H: d.getHours(),
			Mi: d.getMinutes(),
			S: d.getSeconds()
		}
		let hDay = {
			year: obj.Y,
			day: this.handleNum(obj.M) + "月" + this.handleNum(obj.D) + "日",
			time: this.handleNum(obj.H) + ":" + this.handleNum(obj.Mi)
		}
		return hDay;
	},

	testWebsocket() {
		let token = uni.getStorageSync('tokenValue')
		let userInfo = uni.getStorageSync('userInfo')
		console.log('testWebsocket==', token, userInfo)
		if (token && userInfo) {
			let url = `${http.baseWebsocketUrl}${userInfo.id}?satoken=${token}&terminalType=2&loginType=user&productId=13`
			// let url=''
			// console.log('url=',url)	
			if (!ws) {
				ws = new WebSocketClient(url);
			} else {
				ws.setUrl(url)
			}
			// 连接
			ws.connect();
			ws.onmessage((data) => {
				// console.log('接受 WebSocket 消息==', data);
			});
		}
	},
	closeWebsocket() {
		if (ws) {
			console.log('closeWebsocket==', ws)
			ws.close();
		}
	},
	resetData() {
		let data = {
			car_id: '',
			ziti: '',
			quche_address: '',
			quche_longitude: '',
			quche_latitude: '',
			huanche_address: '',
			huanche_longitude: '',
			huanche_latitude: '',
			start_time: '',
			end_time: '',
			days: '',
			car_rental_deposit: '',
			illegal_deposit: '',
			isxcb: '',
			xcb_price: '',
			songche_price: '',
			basic_guarantee_fee: '',
			zong_price: '',
			userid: '',
			rent_user: '',
		}
		return data;
	}
}